package com.iss.lxhproject.controller;

import com.iss.lxhproject.entity.User;
import com.iss.lxhproject.model.MyResponseData;
import com.iss.lxhproject.model.UserWithRole;
import com.iss.lxhproject.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;
@RestController
@RequestMapping("user/")
public class UserController {
    @Autowired
    private UserService service;
    @RequestMapping("login")
    public MyResponseData login(User user, HttpSession session){
        MyResponseData data = new MyResponseData();
        System.out.println(user);
       UserWithRole loginUser = service.login(user);
        session.setAttribute("loginuser",loginUser);
        if(loginUser==null){
            data.setErrorCode(-1);
            data.setStatus("用户名或密码不正确");
        } else{
            data.setErrorCode(0);
            data.setStatus("登录成功");
            data.setObject(loginUser);
        }
        return data;
    }
    @RequestMapping("getUserInfo")
    public MyResponseData getUserInfo(HttpSession session){
        MyResponseData data = new MyResponseData();
        UserWithRole loginUser = (UserWithRole)session.getAttribute("loginuser");
        data.setObject(loginUser);
        System.out.println(data);
        return data;
    }
    @RequestMapping("CheckUserNameExits")
    public MyResponseData CheckUserNameExits(String u_name) {
        MyResponseData data = new MyResponseData();
        boolean flag = service.CheckUserName(u_name);
        if(flag){
            data.setErrorCode(-1);
            data.setStatus("用户名已存在");
        } else{
            data.setErrorCode(0);
        }
        return data;
    }

    @RequestMapping("register")
    public MyResponseData register(@Param("u_id") String u_id, @Param("u_name") String u_name, @Param("u_psd") String u_psd, HttpServletRequest request,
                                   HttpServletResponse response){
        MyResponseData data = new MyResponseData();
        u_id= UUID.randomUUID().toString();
        int u_roleid= Integer.parseInt(request.getParameter("u_roleid"));
        User user = new User(u_id,u_name,u_psd,u_roleid);
        System.out.println(user);
        Boolean flag = service.register(u_id,u_name,u_psd,u_roleid);
        System.out.println(flag);
        if(flag){
            data.setErrorCode((0));
            data.setStatus("注册成功");
            try {
                response.sendRedirect("/gotoregister");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            data.setErrorCode(-1);
            data.setStatus("注册失败");
        }
        return data;
    }
    @RequestMapping("updateUserpwd")
    public MyResponseData updateUserpwd(@Param("u_name") String u_name,@Param("u_psd") String u_psd1,@Param("u_phone") String u_phone){
        MyResponseData data = new MyResponseData();
        System.out.println(u_name);
        System.out.println(u_psd1);
        System.out.println(u_phone);
        Boolean flag = service.updateUserpwd(u_name,u_psd1,u_phone);
        if(flag){
            data.setErrorCode(0);
            data.setStatus("密码修改成功");
        }else{
            data.setErrorCode(-1);
        }
        return data;
    }
}
